package com.tf.integrated.controller;

import com.alibaba.fastjson.JSONObject;
import com.ccssoft.m8.common.ccssoft.modules.util.EntityWrapperHandle;
import com.ccssoft.m8.common.web.BaseController;
import com.tf.integrated.entity.EventresultbikesinfoEntity;
import com.tf.integrated.entity.SearchDataReturnEntity;
import com.tf.integrated.service.EventresultbikesinfoService;
import com.tf.integrated.service.EventresultpedestriansinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.regex.Pattern;

/**
 * 两轮车检索
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "两轮车检索",description="SearchBikeController")
@RestController
public class SearchBikeController extends BaseController
{
    @Autowired
    private EventresultbikesinfoService<EventresultbikesinfoEntity> bikeService;

    @ApiOperation("两轮车检索")
    @GetMapping("/search/bikes")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public SearchDataReturnEntity<JSONObject[]> searchDataList(@RequestParam Map<String, Object> data) {
        if (data != null && data.size() > 0) {
            String starttime=(String)data.get("starttime");
            String endtime=(String)data.get("endtime");
            String spage=(String)data.get("page");
            String ssize=(String)data.get("size");
            int page=0;
            int size=0;
            if(isExist(spage)&&isInteger(spage)){
                page=Integer.parseInt(spage);
            }
            if(isExist(ssize)&&isInteger(ssize)){
                size=Integer.parseInt(ssize);
            }
            int total=0;
            JSONObject[] arr=new JSONObject[0];
            if(isExist(starttime)&&isExist(endtime)&&page>=1&&size>0&&size<=200){
                int begin=(page-1)*size;
                int end=begin+size-1;
                EntityWrapperHandle<EventresultbikesinfoEntity> ew = new EntityWrapperHandle<EventresultbikesinfoEntity>();
                EntityWrapperHandle<EventresultbikesinfoEntity> ew3 = new EntityWrapperHandle<EventresultbikesinfoEntity>();
                ew.orderDesc(Collections.singletonList("id"));
                ew.last("limit "+(end+1));
                ew.between("createtime",starttime,endtime);
                ew3.between("createtime",starttime,endtime);
                String[] hosts=getStringArray((String)data.get("host"));
                if(hosts.length>0){
                    String sql=getSqlAndEqualOr("deviceid",hosts);
                    ew.and(sql);
                    ew3.and(sql);
                }
                String[] chnIds=getStringArray((String)data.get("chnId"));
                if(chnIds.length>0){
                    String sql=getSqlAndEqualOr("chn",chnIds);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] type=getIntArray((String)data.get("type"));
                if(type.length>0){
                    String sql=getJsonIdAndEqualOr("type",type);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] sex=getIntArray((String)data.get("sex"));
                if(sex.length>0){
                    String sql=getJsonIdAndEqualOr("sex",sex);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] age=getIntArray((String)data.get("age"));
                if(age.length>0){
                    String sql=getJsonIdAndEqualOr("age",age);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] orientation=getIntArray((String)data.get("orientation"));
                if(orientation.length>0){
                    String sql=getJsonIdAndEqualOr("orientation",orientation);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] umbrella=getIntArray((String)data.get("umbrella"));
                if(umbrella.length>0){
                    String sql=getJsonIdAndEqualOr("umbrella",umbrella);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] hair=getIntArray((String)data.get("hair"));
                if(hair.length>0){
                    String sql=getJsonIdAndEqualOr("hair",hair);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] hat=getIntArray((String)data.get("hat"));
                if(hat.length>0){
                    String sql=getJsonIdAndEqualOr("hat",hat);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] shoulderbag=getIntArray((String)data.get("shoulderbag"));
                if(shoulderbag.length>0){
                    String sql=getJsonIdAndEqualOr("shoulderaag",shoulderbag);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] bag=getIntArray((String)data.get("bag"));
                if(bag.length>0){
                    String sql=getJsonIdAndEqualOr("bag",bag);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] mask=getIntArray((String)data.get("mask"));
                if(mask.length>0){
                    String sql=getJsonIdAndEqualOr("mask",mask);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] glasses=getIntArray((String)data.get("glasses"));
                if(glasses.length>0){
                    String sql=getJsonIdAndEqualOr("glasses",glasses);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] knapsack=getIntArray((String)data.get("knapsack"));
                if(knapsack.length>0){
                    String sql=getJsonIdAndEqualOr("knapsack",knapsack);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] baby=getIntArray((String)data.get("baby"));
                if(baby.length>0){
                    String sql=getJsonIdAndEqualOr("baby",baby);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] messengerbag=getIntArray((String)data.get("messengerbag"));
                if(messengerbag.length>0){
                    String sql=getJsonIdAndEqualOr("messengerbag",messengerbag);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] uppertexture=getIntArray((String)data.get("uppertexture"));
                if(uppertexture.length>0){
                    String sql=getJsonIdAndEqualOr("uppertexture",uppertexture);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] uppercolor=getIntArray((String)data.get("uppercolor"));
                if(uppercolor.length>0){
                    String sql=getJsonIdAndEqualOr("uppercolor",uppercolor);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] uppertype=getIntArray((String)data.get("uppertype"));
                if(uppertype.length>0){
                    String sql=getJsonIdAndEqualOr("uppertype",uppertype);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] bottomcolor=getIntArray((String)data.get("bottomcolor"));
                if(bottomcolor.length>0){
                    String sql=getJsonIdAndEqualOr("bottomcolor",bottomcolor);
                    ew.and(sql);
                    ew3.and(sql);
                }
                Integer[] bottomtype=getIntArray((String)data.get("bottomtype"));
                if(bottomtype.length>0){
                    String sql=getJsonIdAndEqualOr("bottomtype",bottomtype);
                    ew.and(sql);
                    ew3.and(sql);
                }
                List<EventresultbikesinfoEntity> list = bikeService.selectList(ew);
                total=bikeService.selectCount(ew3);
                if(list!=null&&list.size()>0){
                    if (list.size() - 1 < begin) {
                        list.clear();
                    } else if (list.size() - 1 >= begin ) {
                        list=list.subList(begin,list.size());
                    }
                    arr=new JSONObject[list.size()];
                    for(int i=0;i<list.size();i++) {
                        JSONObject json = new JSONObject();
                        json.put("id", list.get(i).getId());
                        json.put("Age", list.get(i).getAge());
                        json.put("Baby", list.get(i).getBaby());
                        json.put("Bag", list.get(i).getBag());
                        json.put("BottomColor", list.get(i).getBottomcolor());
                        json.put("BottomType", list.get(i).getBottomtype());
                        json.put("Chn", list.get(i).getChn());
                        json.put("DriveWay", list.get(i).getDriveway());
                        json.put("FullPicPath", list.get(i).getFullpicpath());
                        json.put("Glasses", list.get(i).getGlasses());
                        json.put("Hair", list.get(i).getHair());
                        json.put("Hat", list.get(i).getHat());
                        json.put("Knapsack", list.get(i).getKnapsack());
                        json.put("Mask", list.get(i).getMask());
                        json.put("MessengerBag", list.get(i).getMessengerbag());
                        json.put("ObjPicPath", list.get(i).getObjpicpath());
                        json.put("Orientation", list.get(i).getOrientation());
                        json.put("Rect", list.get(i).getRect());
                        json.put("Sex", list.get(i).getSex());
                        json.put("ShoulderBag", list.get(i).getShoulderaag());
                        json.put("Time", list.get(i).getTime());
                        json.put("Type", list.get(i).getType());
                        json.put("Umbrella", list.get(i).getUmbrella());
                        json.put("UpperColor", list.get(i).getUppercolor());
                        json.put("UpperTexture", list.get(i).getUppertexture());
                        json.put("UpperType", list.get(i).getUppertype());
                        json.put("DeviceId",list.get(i).getDeviceid());
                        arr[i] = json;
                    }
                }
            }
            return new SearchDataReturnEntity<JSONObject[]>().ok(arr,200,total);
        }
        return new SearchDataReturnEntity<JSONObject[]>().error("size or page err");
    }

    public boolean isExist(String value){
        return value != null && !value.equals("") ;
    }

    public String[] getStringArray(String s){
        if(isExist(s)){
            return s.split(",");
        }
        return new String[0];
    }

    public Integer[] getIntArray(String s){
        if(!isExist(s)){
            return new Integer[0];
        }
        String[] str = s.split(",");
        List<Integer> list = new ArrayList<>();
        for (String value : str) {
            if (isInteger(value)) {
                list.add(Integer.parseInt(value));
            }
        }
        return list.toArray(new Integer[0]);
    }

    public boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    public String getSqlAndEqualOr(String field,String[] values){
        StringBuilder sql= new StringBuilder();
        sql.append("(");
        for(int i=0;i<values.length;i++){
            sql.append(field).append("='").append(values[i]).append("'");
            if(i<values.length-1){
                sql.append(" or ");
            }else{
                sql.append(")");
            }
        }
        return sql.toString();
    }

    public String getJsonIdAndEqualOr(String field,Integer[] values){
        StringBuilder sql= new StringBuilder();
        sql.append("(");
        for(int i=0;i<values.length;i++){
            sql.append("JSON_UNQUOTE(JSON_EXTRACT(").append(field).append(",'$.Id'))=").append(values[i]);
            if(i<values.length-1){
                sql.append(" or ");
            }else{
                sql.append(")");
            }
        }
        return sql.toString();
    }
}